/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: ScrollDialog.java 46 2008-05-15 18:09:39Z jens464 $
*/
package albareth.frontend.slick.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;

public abstract class ScrollDialog extends GlidingScroll implements
    UIButtonListener
{
    private static Color _textColor = new Color(120, 18, 0);
    private static Color _titleColor = Color.black;
    private static int _paddingLeft = 25;
    private static int _paddingTop = 25;
    private Object[] _textLines = null;
    private String _title = "";
    private UIButton _okButton;


    public ScrollDialog(String title, int start, SlickFrontend frontend)
    {
        super(start, frontend);

        setForegroundColor(_textColor);
        
        _okButton = new UIButton(510, 237, 89, 27);
        _okButton.setForegroundColor(Color.yellow);
        _okButton.setText("OK");
        _okButton.setPadding(25, 4);
        _okButton.setMouseUpImage(SlickResources.BUTTON);
        _okButton.setMouseDownImage(SlickResources.BUTTON_PRESSED);
        _okButton.setMouseOverImage(SlickResources.BUTTON_HOVER);
        _okButton.addButtonListener(this);
        setFont(SlickResources.DEFAULT_FONT);
        setTitle(title);
        addChild(_okButton);

    }
    

    public void setTitle(String title)
    {
        _title = title;
    }
    
    public void setText(String text)
    {
        _textLines = UIToolkit.breakLines(
                text, getWidth() - _paddingLeft, getFont()
        );
    }
    
    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        super.render(g, container, screenX, screenY);
        
        g.setColor(_titleColor);
        g.drawString(
                _title,
                screenX + UIToolkit.getCenterStringX(
                        _title, getWidth(), getFont()
                ),
                screenY + _paddingTop
        );

        if (_textLines == null) {
            return;
        }
        
        g.setColor(_textColor);
        for (int i = 0; i < _textLines.length; i++)
        {
            String line = (String)_textLines[i];
            g.drawString(line, screenX + _paddingLeft, screenY + _paddingTop + i*20 + 50);
        }
    }


    public void buttonPressed(UIButton button)
    {
        okButtonPressed();
    }
    
    public void closeDialog()
    {
        getUIManager().removeChild(this);
    }

    protected abstract void okButtonPressed();

}
